import { usePageRequest, useRequest } from "../../../../utils/request";
import dayjs from "../../../../utils/day";

function pretreatment(data) {
    data.list = data.list.map(item => {
        item.start = dayjs.unix(item.live_in_time).format('MM.DD')
        item.end = dayjs.unix(item.live_out_time).format('MM.DD')
        return item
    })
    return data
}

Component({
    properties: {},
    data: {
        status: 0,
        statusMap: {
            1: '待支付',
            2: '待入住',
            3: '入住中',
            4: '退房中',
            5: '已退房',
        }
    },
    methods: {
        onLoad() {
            this.allListRequest = new usePageRequest('api/ownerorderalllist', { pretreatment }, this)
            this.beforeRequest = new usePageRequest('api/ownerorderwaitlivelist', { pretreatment, hold: true }, this)
            this.inRequest = new usePageRequest('api/ownerorderliveinlist', { pretreatment, hold: true }, this)
            this.afterRequest = new usePageRequest('api/ownerorderliveoutlist', { pretreatment, hold: true }, this)
            this.retreatRequest = new useRequest('api/ownerorderback', { hold: true, loading: false }, this)
            this.removeRequest = new useRequest('api/ownerdelorder', { hold: true, loading: false }, this)
        },
        changeStatus(e) {
            const index = Number(e.detail.index)
            this.setData({
                status: index
            })
            wx.startPullDownRefresh()
        },
        onReachBottom() {
            switch (this.data.status) {
                case 0:
                    this.allListRequest.next()
                    break
                case 1:
                    this.beforeRequest.next()
                    break
                case 2:
                    this.inRequest.next()
                    break
                case 3:
                    this.afterRequest.next()
                    break
            }
        },
        onPullDownRefresh() {
            switch (this.data.status) {
                case 0:
                    this.allListRequest.reset().then(() => {
                        wx.stopPullDownRefresh()
                    })
                    break
                case 1:
                    this.beforeRequest.reset().then(() => {
                        wx.stopPullDownRefresh()
                    })
                    break
                case 2:
                    this.inRequest.reset().then(() => {
                        wx.stopPullDownRefresh()
                    })
                    break
                case 3:
                    this.afterRequest.reset().then(() => {
                        wx.stopPullDownRefresh()
                    })
                    break
            }
        },
        retreat(e) {
            const { index } = e.currentTarget.dataset
            wx.showModal({
                title: '是否申请退房',
                content: '退房之后将不可再入住',
                success: async result => {
                    if (result.confirm) {
                        wx.showLoading({})
                        await this.retreatRequest.run({ order_no: this.data.data[index].order_no })
                        wx.hideLoading()
                    }
                }
            })
        },
        toComment(e) {
            const { index } = e.currentTarget.dataset
            wx.navigateTo({
                url: '/pages/house/comment/comment?id=' + this.data.data[index].house_id
            })
        },
        remove(e) {
            const { index } = e.currentTarget.dataset
            wx.showModal({
                title: '是否删除该订单',
                success: async result => {
                    if (result.confirm) {
                        wx.showLoading({})
                        await this.removeRequest.run({ order_no: this.data.data[index].order_no })
                        wx.hideLoading()
                    }
                }
            })
        },
        callPhone(e) {
            const { phone } = e.currentTarget.dataset
            wx.makePhoneCall({
                phoneNumber: phone
            })
        },
    }
});
